package service.impl;

import common.Constants.ServiceConstant;
import common.Result;
import dao.CourseInfoDao;
import dao.ScoresDao;
import dataBean.CourseInfoDo;
import dataBean.ScoresDo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by lxl on 2015/10/30.
 */
public class CourseServiceImpl implements service.CourseService {

    @Autowired
    private CourseInfoDao courseInfoDao ;

    @Autowired
    private ScoresDao scoresDao ;

    @Override
    public Result addCourse(CourseInfoDo courseInfoDo) {

        Result result = new Result() ;

        try {

            courseInfoDao.insertCourseInfo(courseInfoDo) ;

        }catch ( Exception e ){

            System.out.println(e.toString());

            result.setError(ServiceConstant.COURSE_INSERT_ERROR);
            result.setSucces(false);

            return result ;

        }

        result.setSucces(true);

        return result;

    }

    @Override
    public Result addStudentList(CourseInfoDo courseInfoDo, List<Integer> students) {

        Result result = new Result() ;

        result.setSucces(true);

        for ( int i = 0 ; i < students.size() ; i++){

            ScoresDo scoresDo = new ScoresDo() ;

            scoresDo.setCourseID(courseInfoDo.getId());
            scoresDo.setUserID(students.get(i)) ;

            try {

                scoresDao.insertScores(scoresDo) ;

            }catch ( Exception e ){

                System.out.println(e.toString());

                result.setSucces(false);
                result.setError(ServiceConstant.STUDENT_INSERT_ERROR);
                result.addParam(String.valueOf(students.get(i)),null);

            }

        }

        return result;
    }
}
